//
//  ViewController.m
//  10508TOM猫
//
//  Created by student on 16/2/19.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
{
    SystemSoundID ToMSoundID;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(copy,nonatomic)NSString *str;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}
- (IBAction)btnPressed:(UIButton *)sender {
    NSMutableArray *mArr=[[NSMutableArray alloc]init];
    switch (sender.tag) {
        case 0:
            mArr=[self imageChoose:40 :@"eat"];
            break;
        case 1:
            mArr=[self imageChoose:81 :@"drink"];
            break;
        case 2:
            mArr=[self imageChoose:56 :@"scratch"];
            break;
        case 3:
            mArr=[self imageChoose:24 :@"pie"];
            break;
        case 4:
            mArr=[self imageChoose:28 :@"fart"];
            break;
        case 5:
            mArr=[self imageChoose:13 :@"cymbal"];
            break;
        case 6:
            mArr=[self imageChoose:81 :@"knockout"];
            break;
        case 7:
            mArr=[self imageChoose:26 :@"angry"];
            break;
        case 8:
            mArr=[self imageChoose:34 :@"stomach"];
            break;
        case 10:
            mArr=[self imageChoose:30 :@"footLeft"];
            break;
        case 9:
            mArr=[self imageChoose:30 :@"footRight"];
            break;
        default:
            break;
    }
    self.imageView.animationRepeatCount=1;
    self.imageView.animationImages=mArr;
    if (mArr.count>40) {
        self.imageView.animationDuration=4;
    }
    else{
        self.imageView.animationDuration=2;
    }
    [self.imageView startAnimating];
    sleep(2);
    [self playTomSound];
}

//图片数组
-(NSMutableArray *)imageChoose:(int)num :(NSString *)str{
    NSMutableArray *mArr=[[NSMutableArray alloc]init];
   self.str=str;
    for (int i=0; i<num; i++) {
        NSString *picName=[NSString stringWithFormat:@"%@_%02i",str,i];
        UIImage *image=[UIImage imageNamed:picName];
        [mArr addObject:image];
    }
    return mArr;
}

-(void)playTomSound
{
    NSURL *soundURL=[[NSBundle mainBundle]URLForResource:self.str withExtension:@"m4a"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef  _Nonnull) (soundURL), &ToMSoundID);
    AudioServicesPlayAlertSound(ToMSoundID);
}
-(void)setStr:(NSString *)str
{
    _str=str;
}
@end
